import {
  get,
  post
} from '@/utils/http'

// 登录
export const doLoginApi = userData => post('/api/login', userData)

// 头像上传
export const uploadAvatar = formData => post('/api/upfile', formData, {
  headers: {
    'content-type': 'multiparty/form-data'
  }
})

// 添加用户
export const addUserApi = userData => post('/api/userAdd', userData)

// 用户修改
export const editUserApi = (userData, id) => post('/api/userEdit/' + id, userData)

// 用户列表
// export const getUserListApi = (page = 1) => get('/api/users?page=' + page)
export const getUserListApi = async (page = 1) => {
  let ret = await get('/api/users', {
    params: {
      page
    }
  })
  let users = ret.data.users
  let total = ret.data.total
  // users = users.map(item => ({
  //   ...item,
  //   sex: item.sex == 1 ? '男' : '女'
  // }))
  return {
    users,
    total
  }
}


// 删除所中用户
export const deleteUserApi = userIds => post('/api/delAllusers', {
  ids: userIds
})

// 根据用户id返回对应用户信息
export const showUserInfoApi = id => get('/api/users/' + id)

// 根据用户的角色来决定当前用户是否有显示的对应的操作按钮权限
export const checkRoleBtnActionApi = (roleid, action) => post('/api/btnRole', {
  roleid,
  action
})


export const exportAllExcelApi = () => {
  get('/users.xlsx', {
    responseType: 'blob'
  }).then(blob => {
    const href = window.URL.createObjectURL(blob)
    const aDom = document.createElement('a')
    aDom.href = href
    aDom.style.display = 'none'
    aDom.setAttribute('download', 'download.xlsx')
    document.body.appendChild(aDom)
    aDom.click()
    aDom.remove()
    window.URL.revokeObjectURL(href)
  })
}

export const importExcelDataSendRemoteApi = userData => post('/api/importExcel', userData)